/**
  ******************************************************************************
  * @file    subghz_phy_app.h
  * @author  MCD Application Team
  * @brief   Header of application of the SubGHz_Phy Middleware
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2022 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */

#ifndef __SUBGHZ_PHY_APP_H__
#define __SUBGHZ_PHY_APP_H__

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdbool.h>
/* USER CODE END Includes */

/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */
/* USER CODE END ET */

/* Exported constants --------------------------------------------------------*/
#define USE_MODEM_LORA  1
#define USE_MODEM_FSK   0

#define REGION_CN470

#if defined( REGION_AS923 )
#undef RF_FREQUENCY
#define RF_FREQUENCY                                923000000 /* Hz */
#elif defined( REGION_AU915 )
#undef RF_FREQUENCY
#define RF_FREQUENCY                                915000000 /* Hz */
#elif defined( REGION_CN470 )
#undef RF_FREQUENCY
#define RF_FREQUENCY                                470000000 /* Hz */
#elif defined( REGION_CN779 )
#undef RF_FREQUENCY
#define RF_FREQUENCY                                779000000 /* Hz */
#elif defined( REGION_EU433 )
#undef RF_FREQUENCY
#define RF_FREQUENCY                                433000000 /* Hz */
#elif defined( REGION_EU868 )
#undef RF_FREQUENCY
#define RF_FREQUENCY                                868000000 /* Hz */
#elif defined( REGION_KR920 )
#undef RF_FREQUENCY
#define RF_FREQUENCY                                920000000 /* Hz */
#elif defined( REGION_IN865 )
#undef RF_FREQUENCY
#define RF_FREQUENCY                                865000000 /* Hz */
#elif defined( REGION_US915 )
#undef RF_FREQUENCY
#define RF_FREQUENCY                                915000000 /* Hz */
#elif defined( REGION_RU864 )
#undef RF_FREQUENCY
#define RF_FREQUENCY                                864000000 /* Hz */
#else
#error "Please define a frequency band in the compiler options."
#endif

#define TX_OUTPUT_POWER                             22        /* dBm */

#if (( USE_MODEM_LORA == 1 ) && ( USE_MODEM_FSK == 0 ))
#undef LORA_BANDWIDTH
#define LORA_BANDWIDTH                              0         /* [0: 125 kHz, 1: 250 kHz, 2: 500 kHz, 3: Reserved] */
#define LORA_SPREADING_FACTOR                       7         /* [SF7..SF12] */
#define LORA_CODINGRATE                             1         /* [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8] */
#define LORA_PREAMBLE_LENGTH                        8         /* Same for Tx and Rx */
#define LORA_SYMBOL_TIMEOUT                         5         /* Symbols */
#define LORA_FIX_LENGTH_PAYLOAD_ON                  false
#define LORA_IQ_INVERSION_ON                        false
#elif (( USE_MODEM_LORA == 0 ) && ( USE_MODEM_FSK == 1 ))
#define FSK_FDEV                                    25000     /* Hz */
#define FSK_DATARATE                                50000     /* bps */
#define FSK_BANDWIDTH                               50000     /* Hz */
#define FSK_PREAMBLE_LENGTH                         5         /* Same for Tx and Rx */
#define FSK_FIX_LENGTH_PAYLOAD_ON                   false
#else
#error "Please define a modem in the compiler subghz_phy_app.h."
#endif

#define PAYLOAD_LEN                                 64

/* USER CODE BEGIN EC */
/* Communication Protocol Definitions */
#define MASTER_ID                                   'M'
#define SLAVE_BASE_ID                               '1'       // Slave IDs: '1', '2', '3', '4'
#define MAX_SLAVES                                  4
#define POLLS_PER_SLAVE                             1         // Poll each slave this many times before moving to next

/* Command Types */
#define POLL_CMD                                    'P'
#define RESPONSE_CMD                                'R'
#define ACK_CMD                                    'A'

/* Timeout Values */
#define POLL_TIMEOUT_MS                             500       // Timeout for poll response
#define INTER_POLL_DELAY_MS                         1000      // Delay between poll cycles
#define ACK_TIMEOUT_MS                              200       // Timeout for ACK
#undef TX_TIMEOUT_VALUE
#define TX_TIMEOUT_VALUE                            200
/* Retry Count */
#define MAX_RETRIES                                 2         // Max retries for failed polls
/* USER CODE END EC */

/* External variables --------------------------------------------------------*/
/* USER CODE BEGIN EV */
/* USER CODE END EV */

/* Exported macros -----------------------------------------------------------*/
/* USER CODE BEGIN EM */
/* USER CODE END EM */

/* Exported functions prototypes ---------------------------------------------*/
void SubghzApp_Init(void);
/* USER CODE BEGIN EFP */
/* USER CODE END EFP */

#ifdef __cplusplus
}
#endif

#endif /*__SUBGHZ_PHY_APP_H__*/